package 力扣_滑动窗口.第三类模板;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author zx
 * @create 2022-05-09 9:35
 */
public class 找到字符串中所有字母异位词_438 {
    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> res = new ArrayList<>();
        if(s.length() < p.length()){
            return res;
        }
        int[] arr1 = new int[26];
        int[] arr2 = new int[26];
        int windowSize = p.length();
        for(int i = 0;i < windowSize;i++){
            arr1[s.charAt(i) - 'a']++;
            arr2[p.charAt(i) - 'a']++;
        }
        if(Arrays.equals(arr1,arr2)){
            res.add(0);
        }
        int left = 0;
        for(int right = windowSize;right < s.length();right++){
            arr1[s.charAt(right) - 'a']++;
            left = right - windowSize;//窗口左值是固定的：right - window
            arr1[s.charAt(left) - 'a']--;
            if(Arrays.equals(arr1,arr2)){
                res.add(left + 1);
            }
        }
        return res;
    }
}
